package com.cxhc.system.service.app.impl;

import java.util.List;

import com.cxhc.common.core.domain.entity.SysUser;
import com.cxhc.common.utils.SecurityUtils;
import com.cxhc.system.domain.app.ApprovalFlow;
import com.cxhc.system.domain.app.EventApproval;
import com.cxhc.system.domain.app.EventApprovalFile;
import com.cxhc.system.dto.EventApprovalDto;
import com.cxhc.system.mapper.app.ApprovalFlowMapper;
import com.cxhc.system.mapper.app.EventApprovalFileMapper;
import com.cxhc.system.mapper.app.EventApprovalMapper;
import com.cxhc.system.service.app.IEventApprovalService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 事项报备审批Service业务层处理
 *
 * @author Dhq
 * @date 2024-08-06
 */
@Service
public class EventApprovalServiceImpl implements IEventApprovalService {
    @Resource
    private EventApprovalMapper eventApprovalMapper;
    @Resource
    private ApprovalFlowMapper approvalFlowMapper;
    @Resource
    private EventApprovalFileMapper eventApprovalFileMapper;

    /**
     * 查询事项报备审批
     *
     * @param id 事项报备审批主键
     * @return 事项报备审批
     */
    @Override
    public EventApproval selectEventApprovalById(Long id) {
        return eventApprovalMapper.selectEventApprovalById(id);
    }

    @Override
    public EventApproval selectEventApprovalOldById(Long id) {
        return eventApprovalMapper.selectEventApprovalOldById(id);
    }

    /**
     * 查询事项报备审批列表
     *
     * @param eventApproval 事项报备审批
     * @return 事项报备审批
     */
    @Override
    public List<EventApproval> selectEventApprovalList(EventApproval eventApproval) {
        return eventApprovalMapper.selectEventApprovalList(eventApproval);
    }

    @Override
    public List<EventApproval> selectEventApprovalOldList(String userName, String status) {
        return eventApprovalMapper.selectEventApprovalOldList(userName, status);
    }

    /**
     * 新增事项报备审批
     *
     * @param dto 事项报备审批
     * @return 结果
     */
    @Override
    public int insert(EventApprovalDto dto) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        EventApproval eventApproval = new EventApproval().setTitle(dto.getTitle())
                .setContent(dto.getContent())
                .setUserId(user.getUserId())
                .setStartAt(dto.getStartAt())
                .setEndAt(dto.getEndAt())
                .setUserName(user.getNickName())
                .setDeptName(user.getDept().getDeptName());


        int row = eventApprovalMapper.insertEventApproval(eventApproval);
        if (row == 1) {
            // 插入审批的附件列表
            if (dto.getFileList() != null && !dto.getFileList().isEmpty()) {
                dto.getFileList().forEach(f -> {
                    eventApprovalFileMapper.insertEventApprovalFile(new EventApprovalFile()
                            .setEventId(eventApproval.getId())
                            .setFileName(f.getFileName())
                            .setFileUrl(f.getFileUrl()));
                });
            }
            // 下级审批人的记录
            approvalFlowMapper.insertApprovalFlow(new ApprovalFlow()
                    .setEventId(eventApproval.getId())
                    .setUserId(dto.getUserId())
                    .setUserName(dto.getUserName())
                    .setDeptName(dto.getDeptName()));
        }
        return row;

    }


    /**
     * 修改事项报备审批
     *
     * @param eventApproval 事项报备审批
     * @return 结果
     */
    @Override
    public int updateEventApproval(EventApproval eventApproval) {
        return eventApprovalMapper.updateEventApproval(eventApproval);
    }

    /**
     * 批量删除事项报备审批
     *
     * @param ids 需要删除的事项报备审批主键
     * @return 结果
     */
    @Override
    public int deleteEventApprovalByIds(Long[] ids) {
        return eventApprovalMapper.deleteEventApprovalByIds(ids);
    }

    /**
     * 删除事项报备审批信息
     *
     * @param id 事项报备审批主键
     * @return 结果
     */
    @Override
    public int deleteEventApprovalById(Long id) {
        return eventApprovalMapper.deleteEventApprovalById(id);
    }
}
